import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/SPClassToastUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sprintf/sprintf.dart';

class SPClassSignInWidget extends StatefulWidget {
  const SPClassSignInWidget({Key ?key}) : super(key: key);

  @override
  _SPClassSignInWidgetState createState() => _SPClassSignInWidgetState();
}

class _SPClassSignInWidgetState extends State<SPClassSignInWidget> {
  int spProSeqNum=0;//连续打卡天数
  bool spProIsSignIn=false;
  List signList=[
    {"title":"88","sign":false},
    {"title":"148","sign":false},
    {"title":"188","sign":false},
    {"title":"248","sign":false},
    {"title":"288","sign":false},
    {"title":"388","sign":false},
    {"title":"888","sign":false},
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    spFunGetSignList();
  }

  @override
  Widget build(BuildContext context) {
    return signWidget();
  }

  //  签到组件
  Widget signWidget() {
    return Container(
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.symmetric(horizontal: width(10)),
      padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(15)),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow:[
          BoxShadow(
            offset: Offset(0,0),
            color: Color(0xFF000000).withOpacity(0.16),
            blurRadius:width(6,),),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          RichText(
            text: TextSpan(
                text: '连续签到',
                style: TextStyle(fontSize: sp(12),color: MyColors.grey_33),
                children: [
                  TextSpan(
                    text: spProSeqNum.toString(),
                    style: TextStyle(fontSize: sp(15),color: MyColors.red,fontWeight: FontWeight.bold),
                  ),
                  TextSpan(
                    text: '天',
                    style: TextStyle(fontSize: sp(12),color: MyColors.grey_33),
                  ),
                ]
            ),
          ),
          Container(
            padding: EdgeInsets.only(top:width(20),bottom: width(10),left: width(4),right: width(4) ),
            child:Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children:signList.map((item) {
                return Container(
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Container(
                            decoration: BoxDecoration(
                                color: Color(0xFFFFEAB2),
                                border: Border.all(width: 0.4,color: Color(0xFFE5AD6A)),
                                shape: BoxShape.circle,
                                image: (signList.indexOf(item)==spProSeqNum&&!spProIsSignIn)?DecorationImage(
                                    image: AssetImage(
                                      SPClassImageUtil.spFunGetImagePath("ic_sign"),
                                    )
                                ):null
                            ),
                            width: width(27),
                            height: width(27),
                            alignment: Alignment.center,
                            child: Visibility(
                              child:Text(item["title"],
                                style: TextStyle(
                                    color:(signList.indexOf(item)==spProSeqNum&&!spProIsSignIn)?Color(0xFFE66000):Color(0xFFE5AD6A),
                                    fontSize: sp(12)
                                ),),
                              replacement:Image.asset(SPClassImageUtil.spFunGetImagePath("ic_sign_day")),
                              visible: !item["sign"],
                            ),
                          ),
                          SizedBox(height: width(5),),
                          Text(
                            sprintf("第%d天",[(signList.indexOf(item)+1)]),
                            style:TextStyle(fontSize: sp(9)),

                          ),
                        ],
                      ),
                      Visibility(
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal:width(3),vertical: width(13)),
                          width: width(10),
                          height: width(1),
                          color: Color(0xFFE5AD6A),
                        ),
                        visible: signList.indexOf(item)<signList.length-1,
                      ),
                    ],
                  ),
                );
              }).toList(),
            ),
            // child: Row(
            //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //   children: signList.map((e) {
            //     return Column(
            //       children: <Widget>[
            //         Visibility(
            //           child:Container(
            //             width: width(31),
            //             height: width(31),
            //             alignment: Alignment.center,
            //             decoration: BoxDecoration(
            //                 color: Color(0xFFFFEAB2),
            //                 border: Border.all(width: 0.4,color: Color(0xFFE5AD6A)),
            //                 shape: BoxShape.circle
            //             ),
            //             child: Text(e['title'],style: TextStyle(fontSize: sp(12),color:Color(0xFFE5AD6A),),),
            //           ),
            //           replacement:Image.asset(SPClassImageUtil.spFunGetImagePath("ic_sign_day"),width: width(31),),
            //           visible: !e["sign"],
            //         ),
            //         Text('${signList.indexOf(e)+1}天',style: TextStyle(fontSize: sp(12),color: MyColors.grey_33),)
            //       ],
            //     );
            //   }).toList(),
            // ),
          ),
          Center(
            child:GestureDetector(
              child:Visibility(
                child:  Image.asset(SPClassImageUtil.spFunGetImagePath("ic_btn_un_sign"),width: width(120),),
                visible:!spProIsSignIn ,
                replacement:  Image.asset(SPClassImageUtil.spFunGetImagePath("ic_btn_signed"),width: width(120),),
              ),
              onTap: (){
                if(!spProIsSignIn){
                  SPClassApiManager.spFunGetInstance().spFunSignIn(context: context,spProCallBack: SPClassHttpCallBack(
                      spProOnSuccess: (result){
                        SPClassToastUtils.spFunShowToast(msg: '签到成功,领取了${signList[spProSeqNum]['title']}积分');
                        spProIsSignIn=true;
                        spFunGetSignList();
                        SPClassApplicaion.spFunGetUserInfo();
                      }
                  ));
                }

              },
            ),
          )
        ],
      ),
    );
  }

  void spFunGetSignList() {
    SPClassApiManager.spFunGetInstance().spFunSignInSeqNum(spProCallBack: SPClassHttpCallBack(
        spProOnSuccess: (result){
          spProSeqNum= int.tryParse(result.data["seq_num"].toString())!;
          if(result.data["is_sign_in"].toString()=="1"){
            spProIsSignIn=true;
          }else{
            spProSeqNum--;
            spProIsSignIn=false;
          }
          if(spProSeqNum>signList.length+1){
            spProSeqNum=signList.length+1;
          }
          for(var i=0;i<spProSeqNum;i++){
            signList[i]["sign"]=true;
          }
          setState(() {});
        }
    ));
  }

}
